<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2009, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*	Contact form generator plugin
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
	class forms extends kleoplugins implements iplugins{

		public function index($buff,&$buff){
			$this->make_forms($buff,$buff);
		}
		function on_body(){			
		}
		function plugin_install(){
			$this->query("CREATE TABLE IF NOT EXISTS `".KLEO_TABLE_PREFIX."plugins_contact_forms` (
				`formid` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
				`name` VARCHAR( 64 ) NOT NULL ,
				`title` VARCHAR( 64 ) NOT NULL ,
				`desc` TEXT NOT NULL ,
				`email` VARCHAR( 128 ) NOT NULL
				) ENGINE = MYISAM ");
 			$this->query("CREATE TABLE `".KLEO_TABLE_PREFIX."plugins_contact_fields` (
					`fieldid` INT( 11 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					`parent` INT( 11 ) NOT NULL ,
					`name` VARCHAR( 32 ) NOT NULL ,
					`caption` VARCHAR( 32 ) NOT NULL ,
					`type` INT( 1 ) NOT NULL DEFAULT '1' COMMENT '1=text,2=textarea,3=select'
					) ENGINE = MYISAM  ");
		}
		function plugin_remove(){
			$this->query("DROP TABLE `".KLEO_TABLE_PREFIX."plugins_contact_fields`");
			$this->query("DROP TABLE `".KLEO_TABLE_PREFIX."plugins_contact_forms`");
		}
		function make_forms($buff,&$buff){
			if(preg_match('/<!--form\((.+)\)-->(\s+)(.+)(\s+)<!--endform-->/', $buff, $matches)){
				$id = (int)$matches[1];
				$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."plugins_contact_forms LIMIT 1");
				$form_data = $this->fetch_array($query);
				$fbuff .= $matches[3];
die(var_dump($fbuff));
				$this->tpl_set(array(
					"FORMID" => $form_data['formid'],
					"FORM_NAME" => $form_data['name'],
					"FORM_TITLE" => $form_data['title'],
					"FORM_DESCRIPTION" => $form_data['desc'],
					"FORM_EMAIL" => $form_data['email']
				), 
				false, $fbuff, $fbuff);
				$buff = str_replace($matches[0], $fbuff, $buff);
			}
		}
  }


?>